package com.worldstear.service.impl;

import com.worldstear.dao.BaseDBMapper;
import com.worldstear.model.mysql.MysqlVariable;
import com.worldstear.model.mysql.MysqlVersionVariable;
import com.worldstear.service.BaseDBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.*;

@Service
@Slf4j
public class BaseDBServiceImpl implements BaseDBService {
    final BaseDBMapper baseDBMapper;

    public BaseDBServiceImpl(BaseDBMapper baseDBMapper) {
        this.baseDBMapper = baseDBMapper;
    }

    @Override
    public List<String> listDataBase() {
        return baseDBMapper.showDatabases();
    }

    @Override
    public MysqlVersionVariable showVersionVariables() {
        List<MysqlVariable> variables = baseDBMapper.showVersionVariables();
        MysqlVersionVariable mysqlVersionVariable = new MysqlVersionVariable();
        variables.forEach(i -> {
            Arrays.stream(MysqlVersionVariable.class.getDeclaredFields()).forEach(j -> {
                if(Objects.equals(i.getVariableName(), j.getName())) {
                    try {
                        PropertyDescriptor propertyDescriptor = new PropertyDescriptor(i.getVariableName(), mysqlVersionVariable.getClass());
                        Method writeMethod = propertyDescriptor.getWriteMethod();
                        writeMethod.setAccessible(true);
                        writeMethod.invoke(mysqlVersionVariable, i.getValue());
                    } catch (Exception e) {
                        log.error("反射设置值错误,", e);
                    }
                }
            });
        });
        return mysqlVersionVariable;
    }

    @Override
    public Map<String, List<Map<String, String>>> getAllDatabaseObjects(String db) {
        Map<String, List<Map<String, String>>> result = new HashMap<>();
        List<Map<String, String>> mysqlProcedures = baseDBMapper.showProcedures(db);
        result.put("procedure", mysqlProcedures);
        List<Map<String, String>> mysqlFunctions = baseDBMapper.showFunctions(db);
        result.put("function",mysqlFunctions);
        List<Map<String, String>> mysqlViews = baseDBMapper.showViews(db);
        result.put("view",mysqlViews);
        List<Map<String, String>> mysqlTables = baseDBMapper.showTables(db);
        result.put("table",mysqlTables);
        return result;
    }

    @Override
    public Map<String, List<Map<String, String>>> getTableData(Map<String, String> queryParam) {
        HashMap<String, List<Map<String, String>>> result = new HashMap<>();
        List<Map<String, String>> tableColumns = baseDBMapper.getTableColumns(queryParam.get("tableSchema"), queryParam.get("tableName"));
        result.put("tableColumn", tableColumns);
        List<Map<String, String>> tableData = baseDBMapper.getTableData(queryParam.get("tableSchema"), queryParam.get("tableName"));
        result.put("tableData", tableData);
        return result;
    }
}
